/*
 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Oracle or the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */ 

package doru;

import javax.swing.table.AbstractTableModel;






public  class DomainTableModel extends AbstractTableModel {

	
	//declares column names, this will adjust the size of the table automatically, you still need to add a another entry to domain data when it is invoked. 
	private String[] columnNames = {"Domain/IP", "Google SafeSearch", "Norton", "McAfee", "MyWot", "On Block List"};
	//creates first row of table.
	private Object[][] domainData = {{" ", " ", " ", " ", " ", " "}}; 



	public int getColumnCount() {
		return columnNames.length;
	}

	public int getRowCount() {
		return domainData.length;
	}

	public String getColumnName(int col) {
		return columnNames[col];
	}

	public Object getValueAt(int row, int col) {
		
		return domainData[row][col];
	}

	/*
	 * JTable uses this method to determine the default renderer/
	 * editor for each cell.  If we didn't implement this method,
	 * then the last column would contain text ("true"/"false"),
	 * rather than a check box.
	 */
	public Class getColumnClass(int c) {
		return getValueAt(0, c).getClass();
	}

	/**
	 * sets value of array at cell, the method for increasing the size of the array is called here. This could be done more cleanly in another place
	 * but having it done here simplifies the logic for the main thread.
	 */
	public void setValueAt(Object value, int row, int col) {
		if (col == 0)increaseArraySize(); //only increase size of array on first request.
			domainData[row][col] = value;
			}
	
	/**
	 * Increase the size of the array to allow adding new line. I know this could be done in a more elegant way.
	 */
	private void increaseArraySize(){
		
		int currentLength = domainData.length;
		int newLength = ++currentLength;
		int width = columnNames.length;
		
		Object newMulti[][] = new Object[(newLength)][width];
		
		//copies old data to new array.
		for(int i = 0; i < domainData.length; i++){
			for(int j = 0; j < domainData[i].length; j++){
				newMulti[i][j] = domainData[i][j];
			}
		}
		domainData = newMulti; //assigns new array to old pointer
		fireTableStructureChanged();//tell table things have been updated.
	}
}










